package be.vdab.servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.dao.SausenDAO;


@WebServlet("/sausen.htm")
public class SausenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/sausen.jsp";
	private static final String REDIRECT_URL= "/sausen.htm";
	private final SausenDAO sausenDAO = new SausenDAO();

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		request.setAttribute("sausen", sausenDAO.findAll());
		RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
		dispatcher.forward(request, response);		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		try {
		for (String sausNummerAlsString:request.getParameterValues("nummer")) {
			long nummer = Long.parseLong(sausNummerAlsString);
			sausenDAO.removeSaus(nummer);
			
		}}catch (NumberFormatException ex) {}
		response.sendRedirect(request.getContextPath() + REDIRECT_URL );
	}


}
